package com.foo.web.controller.api;

import java.sql.SQLException;
import java.util.List;

import com.foo.common.enums.OperatorType;
import com.foo.wechat.domain.WxPoultry;
import com.foo.wechat.service.IWxPoultryService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxPoultryGrowth;
import com.foo.wechat.service.IWxPoultryGrowthService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;

/**
 * 成长记录Api
 *
 * @author foo
 * @date 2025-11-13
 */
@Controller
@RequestMapping("/wechat/api/WxPoultryGrowth")
public class WxPoultryGrowthApi extends BaseController
{
    @Autowired
    private IWxPoultryGrowthService wxPoultryGrowthService;
    @Autowired
    private IWxPoultryService wxPoultryService;
    /**
     * 查询成长记录列表
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(WxPoultryGrowth wxPoultryGrowth,Integer pageNum, Integer pageSize)
    {
        if(pageNum != null){
            Integer size = 10;
            if(pageSize != null){
                size = pageSize;
            }
            PageHelper.startPage(pageNum, size);
        }
        List<WxPoultryGrowth> list = wxPoultryGrowthService.selectWxPoultryGrowthList(wxPoultryGrowth);
        return AjaxResult.success(list);
    }



    /**
     * 新增成长记录
     */
    @PostMapping("/add")
    @ResponseBody
    @Log(title = "成长记录", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSave(WxPoultryGrowth wxPoultryGrowth) throws Exception
    {
        WxPoultry wxPoultry = wxPoultryService.selectWxPoultryById(wxPoultryGrowth.getGrowthPid());
        if(wxPoultryGrowth.getGrowthKg()>=wxPoultry.getPoultrySlaughterKg()){
            wxPoultry.setPoultrySlaughterState(1L);
            wxPoultryService.updateWxPoultry(wxPoultry);
        }
        if(wxPoultryGrowthService.insertWxPoultryGrowth(wxPoultryGrowth) > 0){
            List<WxPoultryGrowth> wxPoultryGrowthList = wxPoultryGrowthService.selectWxPoultryGrowthList(wxPoultryGrowth);
            if(wxPoultryGrowthList.size() > 0){
                return AjaxResult.success(wxPoultryGrowthList.get(0));
            }else{
                //数据添加成功之后查询不到数据 回滚
                throw new SQLException("数据为空");
            }
        }
        return toAjax(0);
    }

    /**
     * 修改成长记录
     */
    @PostMapping("/edit")
    @ResponseBody
    @Log(title = "成长记录", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editSave(WxPoultryGrowth wxPoultryGrowth)
    {
        WxPoultryGrowth wxPoultryGrowthOld = wxPoultryGrowthService.selectWxPoultryGrowthById(wxPoultryGrowth.getGrowthId());
        WxPoultry wxPoultry = wxPoultryService.selectWxPoultryById(wxPoultryGrowthOld.getGrowthPid());
        if(null!=wxPoultryGrowth.getGrowthKg()){
            if(wxPoultryGrowth.getGrowthKg()>=wxPoultry.getPoultrySlaughterKg()){
                wxPoultry.setPoultrySlaughterState(1L);
                wxPoultryService.updateWxPoultry(wxPoultry);
            }else {
                wxPoultry.setPoultrySlaughterState(0L);
                wxPoultryService.updateWxPoultry(wxPoultry);
            }
        }
        return toAjax(wxPoultryGrowthService.updateWxPoultryGrowth(wxPoultryGrowth));
    }

    /**
     * 删除成长记录(单个)
     */
    @PostMapping( "/remove")
    @ResponseBody
    @Log(title = "成长记录", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(Long id)
    {
        return toAjax(wxPoultryGrowthService.deleteWxPoultryGrowthById(id));
    }

}
